#include "fttextarea.h"
#include "ftcontrols.h"

FTTextArea::FTTextArea(const QString& s, int r, QWidget *parent) :
    FTBasicWidget(parent)
{
    _wordWrap = true;
    nRows = r;
    setText(s);
    if ( nRows == -1 )
        setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Ignored );
    else
        setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Preferred );
    _alignment = Qt::AlignLeft;
}

bool FTTextArea::wordWrap() const
{
    return _wordWrap;
}

void FTTextArea::setWordWrap(bool w)
{
    _wordWrap = w;
}

QString FTTextArea::text() const
{
    return _text;
}

void FTTextArea::setText(const QString & s )
{
    if ( s != _text )
    {
        _text = s;
        update();
    }
}

QSize FTTextArea::sizeHint() const
{
    int padding = FTControls::defaultPadding();
    QFontMetrics fm(font());
    // First, calculate the height for the given number of rows:
    QString tmp = "a";
    for ( int r = 1; r < nRows; r++ )
        tmp += "\na";
    QSize baseSize = fm.size( 0, tmp);
    int flags = Qt::AlignVCenter;
    if ( _wordWrap )
        flags |= Qt::TextWordWrap;
    // Now, calculate the real text bounding rect (if any text has been specified):
    int baseWidth = fm.boundingRect(0,0,9999, baseSize.height(),
                                    flags,
                                     _text).width();
    if ( baseWidth < padding )
        baseWidth = padding;
    baseSize.setWidth( baseWidth );
    QSize size = QSize( padding*2, padding);
    return baseSize + size;
}

QSize FTTextArea::minimumSizeHint() const
{
    QSize size = sizeHint();
    size.setWidth(FTControls::defaultHeight());
    return size;
}

void FTTextArea::paintEvent ( QPaintEvent * )
{
    int padding = FTControls::defaultPadding();
    QPalette palette = QApplication::palette();
    palette.setCurrentColorGroup( FT_COLOR_GROUP );
    QPainter painter(this);
    QRect border = rect().adjusted(1,1,-2,-2);

    int flags = _alignment | Qt::AlignVCenter;
    if ( _wordWrap )
        flags |= Qt::TextWordWrap;

    border = painter.boundingRect( border.adjusted( padding,
                                                    padding>>1,
                                                    -padding,
                                                    -(padding>>1)),
                                    flags,
                                   _text);

    painter.setPen( palette.color( QPalette::WindowText ) );
    if ( _backgroundStyle != Qt::NoBrush )
    {
        QBrush brush = painter.brush();
        brush.setColor( palette.color( QPalette::Window ) );
        brush.setStyle( _backgroundStyle );
        painter.fillRect( rect(), brush );
    }
    painter.drawText( border, _alignment | Qt::AlignVCenter | Qt::TextWordWrap, _text );
}

int FTTextArea::alignment() const
{
    return _alignment;
}

void FTTextArea::setAlignment( int a )
{
    _alignment = a;
    update();
}
